/*
 * Copyright (c) 2018-2024 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#if !defined(_JGRP_H_)
#define _JGRP_H_

#include "lib.h"

#define DESTROY_REF(x, y) { \
     y(x);\
     x = NULL;\
}


typedef void (*FREE_JGARRAY_FUNC_T)(void *);

#define JG_ARRAY_BASE \

struct jgArrayBase {
    int num_reference;
    uid_t uid;
    char *user_name;
    time_t   changeTime;
    void    (*freeJgArray)(void *);
    int counts[NUM_JGRP_COUNTERS + 1];
};

struct jobArray {
    int num_reference;
    uid_t uid;
    char *user_name;
    time_t   changeTime;
    void    (*freeJgArray)(void *);
    int counts[NUM_JGRP_COUNTERS + 1];
    struct job *job_array;
    int max_job_slot;
};

/* job group data
 */
struct jgrpData {
    int num_reference;
    uid_t uid;
    char *user_name;
    time_t submit_time;
    void (*freeJgArray)(void *);
    int counts[NUM_JGRP_COUNTERS + 1];
    int max_job_slot;
};

/* Helper macros
 */
#define JOB_DATA(node)   ((struct job *)node->ndInfo)
#define JGRP_DATA(node)  ((struct jgrpData *)node->ndInfo)
#define ARRAY_DATA(node) ((struct jobArray *)node->ndInfo)

/* Generic node structure, each node then has a void
 * pointer to either a job, job array or job group.
 */
struct jgTreeNode {
    struct jgTreeNode *parent;
    struct jgTreeNode *child;
    struct jgTreeNode *left;
    struct jgTreeNode *right;
    int  nodeType; /* job, jobarray or jobgrp */
    char *name;    /* job or group name */
    char *path;    /* full path */
    void *ndInfo;  /* node is void as it points to different objects */
};

extern char treeFile[];

struct nodeList {
    int isJData;
    void *info;
};

extern struct jgTreeNode *groupRoot;

extern void               treeInit();
extern struct jgTreeNode *treeLexNext(struct jgTreeNode *);
extern struct jgTreeNode *treeLinkSibling(struct jgTreeNode *,
					  struct jgTreeNode *);
extern struct jgTreeNode *treeInsertChild(struct jgTreeNode *,
					  struct jgTreeNode *);
extern void               treeInsertLeft(struct jgTreeNode *,
					 struct jgTreeNode *);
extern void               treeInsertRight(struct jgTreeNode *,
					  struct jgTreeNode *);
extern struct jgTreeNode *treeClip(struct jgTreeNode *);
extern struct jgTreeNode *treeNewNode(int);
extern void               treeFree(struct jgTreeNode *);
extern void               initObj(char *, int);
extern int                getIndexOfJStatus(int );
extern void               updJgrpCountByJStatus(struct job *, int, int);
extern void               putOntoTree(struct job *);
extern int 		  jgrpPermitOk(struct auth *,
				       struct jgTreeNode *);
extern void               updJgrpCountByOp(struct jgTreeNode *, int);
extern void               freeJarray(struct jobArray *);
extern void check_job_dependency(void);
extern int                inIdxList(JOBID_T, struct array_index *);
extern struct jgTreeNode * treeNextSib(struct jgTreeNode *);
extern char *full_jobname_r(struct job *, char *);
extern int    localizeJobElement(struct job *);
extern int    localizeJobArray(struct job *);

#endif
